package io.renren.modules.banner.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.banner.entity.BannerEntity;
import io.renren.modules.banner.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Created by dell on 2021/8/11.
 */


@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 列表
     * */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bannerService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 新增
     * */
    @PostMapping("/save")
    public R save(@RequestBody BannerEntity banner) {
        ValidatorUtils.validateEntity(banner, AddGroup.class);

        bannerService.saveBanner(banner);

        return R.ok();
    }

    /**
     * 修改
     * */
    @RequestMapping("/update")
    public R update(@RequestBody BannerEntity banner) {
        ValidatorUtils.validateEntity(banner, UpdateGroup.class);
        bannerService.update(banner);
        return R.ok();
    }

    /**
     * 详情
     * */
    @RequestMapping("/info/{bannerId}")
    public R info(@PathVariable("bannerId") Long bannerId){

        BannerEntity banner = bannerService.getById(bannerId);
        return R.ok().put("banner", banner);

    }

    /**
     * 删除
     * */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] bannerIds) {
        bannerService.deleteBatch(bannerIds);
        return R.ok();
    }
}
